
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.util.List;
import java.util.Iterator;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;

import FTP.FTPClientExample;

import utiles.AppProperties;

import com.dao.datosVideosDAO;

//import com.file.dao.ExamenFilleDAO;

/**
 * This class provides functionality to handle an uploaded file.
 * This class uses code provided by Apache
 *
 * @author geert.zijlmans
 *
 */
public class ProcessFileUpload extends HttpServlet {

	private static final long serialVersionUID = 1L;
	//private String idExamen;
	private String directorio;
	private String directorio2;
	private String directorio3;		
	String rutaFILE = (AppProperties.getProperty("aplicacion", "RUTA_FILE"));
	//String rutaFILE = "C:\\paso\\";	
	//String rutaFILE = "/PentaCapacita/flvtmp/"; 
	
	/**
	 * Default constructor.
	 */
	public ProcessFileUpload() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			// Check that we have a file upload request
			boolean isMultipart = ServletFileUpload.isMultipartContent(request);
			if (isMultipart) {
				// Create a factory for disk-based file items
				FileItemFactory factory = new DiskFileItemFactory();
				// Create a new file upload handler
				ServletFileUpload upload = new ServletFileUpload(factory);
				// Parse the request
				List /* FileItem */items = upload.parseRequest(request);
				// Process the uploaded items
				Iterator iter = items.iterator();
				while (iter.hasNext()) {
					FileItem item = (FileItem) iter.next();

					if (item.isFormField()) {
						processFormField(item);
					} else {
						processUploadedFile(item);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void processFormField(FileItem item) {
		String name = item.getFieldName();
		String value = item.getString();
		               
		System.out.println("Item name: " + name + " ; value: " + value);
		if (name.equals("ruta1")){
			//idExamen = item.getString();
			directorio = item.getString();			
		}
		if (name.equals("ruta2")){
			directorio2 = item.getString();
		}
		if (name.equals("ruta3")){
			directorio3 = item.getString();
		}
	}

	private void processUploadedFile(FileItem item) throws Exception {
		String fieldName = item.getFieldName();
		String fileName = item.getName();
		String contentType = item.getContentType();
		boolean isInMemory = item.isInMemory();
		long sizeInBytes = item.getSize();
		boolean writeToFile = true;
		if (sizeInBytes > (50 * 1024 * 1024)) {
			//writeToFile = false;
		}
		datosVideosDAO GrabacionDB =  new datosVideosDAO();
		// Process a file upload
		if (writeToFile) {

			File uploadedFile = new File(rutaFILE + fileName);
			System.out.println("sube file en windows:"+fileName);
			if (!uploadedFile.exists()) {
				uploadedFile.createNewFile();
			}
			item.write(uploadedFile);
			FTPClientExample asd =  new FTPClientExample();
			//String directorio = "/Siniestros";
			//String directorio2 = "/Siniestros/dir2";
			//String directorio3 = "/Siniestros/dir2/dir3";			
			
			String remote = directorio3 + "//"+fileName;
			String local = rutaFILE+fileName;
			
			String ftp = "";
			//System.out.println("Remote: "+remote+" local: "+local);
			ftp = asd.Ftpvoid(remote, local, directorio, directorio2, directorio3);
			
		} else {
			System.out.println("Intento escribir un archivo demasiado largo, sobre 10 MB.");
		}
	}
}
